package demo1;


import java.util.Arrays;
import java.util.Comparator;


/*class Student implements Comparable<Student>{

 */
class Student{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    /**
     * 比较方式
     * @param o
     * @return
     */
    /*@Override
    public int compareTo(Student o) {
        //if(this.age > o.age){
        //    return 1;
        //}else if (this.age == o.age){
        //    return 0;
        //}else {
        //    return -1;
        //}
        //return this.age - o.age;
        //return (int)(this.score - o.score);
        return this.name.compareTo(o.name);
    }
    */
}
//年龄比较器
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class ScoreComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score - o2.score);
    }
}

public class TestDemo2 {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("asd",12,67);
        students[1] = new Student("zxc",8,98);
        students[2] = new Student("wer",87,76);

        /*AgeComparator ageComparator = new AgeComparator();
        int ret = ageComparator.compare(students[0],students[1]);
        System.out.println(ret);

        ScoreComparator scoreComparator = new ScoreComparator();
        ret = scoreComparator.compare(students[0],students[1]);
        System.out.println(ret);*/
        AgeComparator ageComparator = new AgeComparator();
        ScoreComparator scoreComparator = new ScoreComparator();
        System.out.println("排序前：" + Arrays.toString(students));
        Arrays.sort(students,ageComparator);
        System.out.println("排序后：" + Arrays.toString(students));
    }
    /*public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("asd",12,67);
        students[1] = new Student("zxc",8,98);
        students[2] = new Student("wer",87,76);
        if (students[0].compareTo(students[1]) > 0){
            System.out.println(">");
        }else {
            System.out.println("<=");
        }
    }

     */

    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("asd",12,67);
        students[1] = new Student("zxc",8,98);
        students[2] = new Student("wer",87,76);
        System.out.println("排序前：" + Arrays.toString(students));
        Arrays.sort(students);
        System.out.println("排序后：" + Arrays.toString(students));
    }
    public static void main1(String[] args) {
        int[] array = {18,23,6,3,7};
        Arrays.sort(array);
        System.out.println("排序后：" + Arrays.toString(array));
    }
}
